<template>
    <el-dialog :close-on-click-modal="false" @open="handleOpen" :modelValue="dialogVisible" @update:modelValue="(e)=>{emit('update:dialogVisible',e)}"   title="Shipping address">
        <el-form :model="formParams">
            <el-form-item label="name" :label-width="formLabelWidth">
                <el-input v-model="formParams.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="isDefault" :label-width="formLabelWidth">
                <el-switch v-model="formParams.isDefault" />
            </el-form-item>
            <el-form-item label="isPublic" :label-width="formLabelWidth">
                <el-switch v-model="formParams.isPublic" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="emit('update:dialogVisible',false)">Cancel</el-button>
                <el-button type="primary" @click="sureClick">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from 'vue'
import {rolesCreate,rolesUpdate} from "@/api/roles.js"
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  dialogVisible: {
    type:Boolean,
    default:false
  },
  routeDatas:{
    type:Array,
    default:function() {
      return []
    }
  },
  type:{
    type:String,
    default:'add'
  },
  editObject:{
    type:Object,
    default:function() {
      return null
    }
  }
})
const emit = defineEmits(['update:dialogVisible','hanldeRequest']);
const formLabelWidth = '140px'
const formParams = ref({
  name:'',
  isDefault:true,
  isPublic:true
})
function handleOpen() {
  if(props.type === 'add') {
    formParams.value = {
      name:'',
      isDefault:true,
      isPublic:true
    }
  }else if(props.type === 'edit') {
    formParams.value = {
      name:props.editObject.name,
      isDefault:props.editObject.isDefault,
      isPublic:props.editObject.isPublic
    }
  }
}
async function sureClick() {
  if(props.type === 'add') {
    await rolesCreate(formParams.value)
    ElMessage({
      type: 'success',
      message: '新增成功',
    })
  }else if(props.type === 'edit') {
    await rolesUpdate({
      roleId:props.editObject.id,
      roleInfo:{...formParams.value,concurrencyStamp:props.editObject.concurrencyStamp}
    })
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  }

  emit('update:dialogVisible',false)
  emit('hanldeRequest')

}
</script>

<style lang="scss" scoped>

</style>